package com.dc.controler;

import com.alibaba.fastjson.JSON;
import com.dc.base.model.BaseModel;
import com.dc.service.impl.UserService;
import com.dc.service.impl.UserServiceImpl;

import javax.jws.WebService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Date;

/**
 * Created by Acer on 2019/9/28.
 */

@WebServlet(name="UserServlet",urlPatterns={"/addUser.do","/updateUser.do","/deleteUser.do","/selectUser.do"})
public class UserContoller extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //做业务逻辑
        String url=req.getRequestURI();
        url=url.substring(url.lastIndexOf("/")+1,url.lastIndexOf("."));
        Method method =null;
        try{
           method = getClass().getDeclaredMethod(url,HttpServletRequest.class,HttpServletResponse.class);
           method.invoke(this,req,resp);
        }catch (Exception e){
            System.out.println("java反射失败");
            e.printStackTrace();
        }
    }
    public void addUser(HttpServletRequest req, HttpServletResponse resp){
        try{
        //定义一个数组
        Object[]  params = new Object[11];
        req.setCharacterEncoding("utf-8");
        //获取用户传入的参数
        params[0]=req.getParameter("name");
        params[1]=req.getParameter("trueName");
        params[2]=req.getParameter("cardId");
        params[3]=req.getParameter("email");
        params[4]=req.getParameter("telephone");
        params[5]=req.getParameter("qqid");
        params[6]=req.getParameter("pwd");
        params[7]=new Date();
        params[8]=req.getParameter("question");
        params[9]=req.getParameter("answer");
        params[10]=1;

            UserService userService = new UserServiceImpl();
            BaseModel baseModel = new BaseModel();
            baseModel=userService.addUser(params);
            String baseModelStr=JSON.toJSONString(baseModel);
            resp.setCharacterEncoding("utf-8");
            resp.setContentType("application/json;charset=utf-8");
            resp.getWriter().write(baseModelStr);
            resp.getWriter().flush();
            resp.getWriter().close();

        }catch (Exception e){
            System.out.println("新增用户失败!");
            e.printStackTrace();
        }
    }
    public void deleteUser(HttpServletRequest req, HttpServletResponse resp){
        try{
            UserService userService = new UserServiceImpl();
            req.setCharacterEncoding("utf-8");
            int id=Integer.valueOf(req.getParameter("id"));
            UserService userService1 = new UserServiceImpl();
            BaseModel baseModel =userService.deleteUser(id);
            String baseModelStr=JSON.toJSONString(baseModel);
            resp.setCharacterEncoding("utf-8");
            resp.setContentType("application/json;charset=utf-8");
            resp.getWriter().write(baseModelStr);
            resp.getWriter().flush();
            resp.getWriter().close();
        }catch (Exception e){
            System.out.println("删除用户信息执行失败");
            e.printStackTrace();
        }
    }

        public void updateUser(HttpServletRequest req, HttpServletResponse resp){
            try{
                UserService userService = new UserServiceImpl();
                //定义一个数组
                Object[]  params = new Object[11];
                req.setCharacterEncoding("utf-8");
                //获取参数
                params[0]=req.getParameter("name");
                params[1]=req.getParameter("trueName");
                params[2]=req.getParameter("cardId");
                params[3]=req.getParameter("email");
                params[4]=req.getParameter("telephone");
                params[5]=req.getParameter("qqid");
                params[6]=req.getParameter("pwd");
                params[7]=new Date();
                params[8]=req.getParameter("question");
                params[9]=req.getParameter("answer");
                params[10]=1;
                params[11]=req.getParameter("id");
                BaseModel baseModel = userService.updateUser(params);
                String baseModelStr=JSON.toJSONString(baseModel);
                resp.setCharacterEncoding("utf-8");
                resp.setContentType("application/json;charset=utf-8");
                resp.getWriter().write(baseModelStr);
                resp.getWriter().flush();
                resp.getWriter().close();
            }catch (Exception e){
                System.out.println("修改用户失败");
                e.printStackTrace();
            }

    }



    public void selectUser(HttpServletRequest req, HttpServletResponse resp){
        UserService userService=new UserServiceImpl();
        BaseModel baseModel=new BaseModel();
        try {
            baseModel=userService.selectUser(null);
            String baseModelStr= JSON.toJSONString(baseModel);
            resp.setCharacterEncoding("utf-8");
            resp.setContentType("application/json;charset=utf-8");

            resp.getWriter().write(baseModelStr);
            resp.getWriter().flush();
            resp.getWriter().close();
        }catch (Exception e){
            System.out.println("查询所有用户信息接口出错");
            e.printStackTrace();
        }

    }

}

